package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

import org.lxh.smart.SmartUpload;
import org.lxh.smart.SmartUploadException;

import entity.Movie;

/**
 * Servlet implementation class AddMovie
 */
@WebServlet("/AddMovie")
public class AddMovie extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddMovie() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("gbk");
		response.setCharacterEncoding("gbk");

		SmartUpload smart = new SmartUpload();
		PageContext pageContext = JspFactory.getDefaultFactory()  
                .getPageContext(this, request, response, null, true, 8192, true);
		smart.initialize(pageContext);
		try {
			smart.upload();
			smart.save("/img");
			
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String filemane = "img/"+smart.getFiles().getFile(0).getFileName(); 
		
		/*String name = request.getParameter("name");
		String type = request.getParameter("type");
		String actor = request.getParameter("actor");
		String length_string = request.getParameter("length");
		String director = request.getParameter("director");
		String price_string = request.getParameter("price");
		String mark_string = request.getParameter("mark");
		String introduce = request.getParameter("introduce");
		//String picture = request.getParameter("picture");
*/		
		/*int length = Integer.parseInt(length_string);
		float price = Float.parseFloat(price_string);
		float mark = Float.parseFloat(mark_string);*/
		
		Movie m = new Movie();
		m.setName(smart.getRequest().getParameter("name"));			
		m.setType(smart.getRequest().getParameter("type"));
		m.setActor(smart.getRequest().getParameter("actor"));
		m.setLength(Integer.parseInt(smart.getRequest().getParameter("length")));
		m.setDirector(smart.getRequest().getParameter("director"));
		m.setPrice(Float.parseFloat(smart.getRequest().getParameter("price")));
		m.setMark(Float.parseFloat(smart.getRequest().getParameter("mark")));
		m.setIntroduce(smart.getRequest().getParameter("introduce"));
		m.setPicture(filemane);	
		System.out.println(smart.getRequest().getParameter("name"));
		service.MovieService am = new service.MovieService();
		am.add(m);
		
		response.sendRedirect("update_movie.jsp");
	}

}
